﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using AMS.DataAccess.Common;

namespace AMS.DataAccess
{
    /// <summary>
    /// 一个 DataTable 与业务对象互相转换的辅助类。
    /// 采用属性名的列名对应的规则. 业务对象的要操作的属性必不需要有 DBAttribute 子类的标注。
    /// 实际上这个类用于平凡实体类的解析.
    /// 等价于 EntityMapping
    /// </summary>
    public static class EntityConverter
    {
        private static EntityMapper mapper = new EntityMapper();

        #region Public Methods
        /// <summary>
        /// 将DataTable转换成与之对应的业务对象列表。
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="table">数据表</param>
        /// <returns>实体列表</returns>
        public static List<T> DataTableToEntityList<T>(DataTable table)
        {
            return mapper.DataTableToEntityList<T>(table);
        }

        /// <summary>
        /// 将一个DataRow转换成一个业务实体对象。
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="row">数据行</param>
        /// <returns>实体列表</returns>
        public static T DataRowToEntity<T>(DataRow row)
        {
            return mapper.DataRowToEntity<T>(row);
        }

        /// <summary>
        /// 将对象的值转到DataRow对应的值中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="row"></param>
        public static void EntityToDataRow<T>(T entity, DataRow row)
        {
            mapper.EntityToDataRow<T>(entity, row);
        }

        /// <summary>
        /// 业务对象列表映射到DataTable
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="entityList">对象列表</param>
        /// <param name="table">数据表</param>
        public static void EntityListToRows<T>(List<T> entityList, DataTable table)
        {
            mapper.EntityListToRows<T>(entityList, table);
        }
        #endregion

    }
}
